package handler

import (
	"api/pkg"
	__ "api/proto"
	"api/request"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
	"path/filepath"
)

func Sendsms(c *gin.Context) {
	var req request.Sendsms
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	sendsms, err := c1.Sendsms(c, &__.SendsmsRequest{
		Tel:   req.Tel,
		Score: req.Score,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "发送短信失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发送短信成功",
		"data": sendsms,
	})
}

func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	login, err := c1.Login(c, &__.LoginRequest{
		Tel:     req.Tel,
		Sendsms: req.Sendsms,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "登录失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": login,
	})
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	//ext := filepath.Ext(file.Filename)
	//if ext != ".MP4" {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 1000,
	//		"msg":  "文件类型不正确",
	//		"data": nil,
	//	})
	//	return
	//}
	if file.Size > 5*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文件过大 请重新上传",
			"data": nil,
		})
		return
	}
	uuid := uuid.NewString()
	dst := "./static/upload" + uuid + filepath.Ext(file.Filename)
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)
	pkg.UploadOss(dst, file.Filename)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": pkg.GetOssFileUrl(file.Filename),
	})

}

func List(c *gin.Context) {
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	login, err := c1.List(c, &__.ListRequest{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "展示失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": login,
	})
}

func Del(c *gin.Context) {
	var req request.Del
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	login, err := c1.Del(c, &__.DelRequest{
		Id: int64(req.Id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "删除失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": login,
	})
}
